import Koa from 'koa';
import jwt from 'koa-jwt'
import { koaBody } from 'koa-body';
import './config/env';
import './config/db';
import { JWT_SECRECT } from './config/jwt';
import router from './routes';
import v1WhiteRoutes from './routes/white';
import { requestLog, errHandle } from './middlewares';
import { redisClient } from './config/cache';

redisClient.connect();

const app = new Koa();
const port = process.env.SERVER_PORT || 3000;

// app.use(async (ctx, next) => {
//   console.log(`${ctx.request.method} ${ctx.request.url}`);
//   await next();
// });

// app.use(async (ctx, next) => {
//   await next();
//   ctx.body = `当前运行环境：${process.env.NODE_ENV}`;
// });

app
  .use(requestLog)
  .use(errHandle)
  .use(
    jwt({secret: JWT_SECRECT})
    .unless({
      path: [
        /\/register/,
        /\/login/,
        ...v1WhiteRoutes,
      ]
    })
  )
  .use(koaBody({
    multipart: true,
    encoding: 'utf8',
    
  }))
  .use(router.routes())
  .use(router.allowedMethods());

app.listen(port);

console.log(`server start at http://localhost:${port}/`);
